package com.shy.propagation;


import org.springframework.aop.framework.AopContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Transactional
    public void test() {
        System.out.println("UserService test");
        // 如果此时需要调用同一个类中的另外一个事务
        //test1(); // 直接调用会导致事务失效

        // 使用AopContext.currentProxy()获取代理对象
        // 使用AopContext需要打开配置 @EnableAspectJAutoProxy(exposeProxy = true)
        UserService userService = (UserService) (AopContext.currentProxy());
        userService.test1();
    }

    @Transactional(propagation = Propagation.NEVER)
    public void test1() {
        System.out.println("UserService test1");
    }

}
